﻿using DatabaseAccess;
using Liguo.AppCode;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace Liguo.Store
{
    public partial class Active : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //使用error code 跳转页面
            String UserName = Request.QueryString["uid"];
            String key = Request.QueryString["key"];
            SqlIt sql = new SqlIt();
            if ((UserName != null && UserName.Length > 0 && Utilities.IsCJKChar(UserName)) && (key != null && key.Length == 64))
            {
                DataSet ds = sql.SpecialItem("UserInfo", "UserName='" + UserName + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]) == 2)
                    {
                        if (ds.Tables[0].Rows[0]["XTime"] != DBNull.Value)
                        {
                            DateTime XTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["XTime"]);
                            if (XTime.AddDays(1).CompareTo(DateTime.Now) > 0)
                            {
                                String XCode = ds.Tables[0].Rows[0]["XCode"].ToString();
                                if (XCode.Equals(key))
                                {
                                    sql.SpecialUpdate("UserInfo", "Status='1'", "UserName='" + UserName + "'");
                                    User user = new User();
                                    user.UserID = (Int32)ds.Tables[0].Rows[0]["ID"];
                                    user.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                                    user.MailBox = ds.Tables[0].Rows[0]["MailBox"].ToString();
                                    user.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                                    user.Salt = ds.Tables[0].Rows[0]["Salt"].ToString();
                                    user.Status = 1;
                                    user.Score = (Int32)ds.Tables[0].Rows[0]["Score"];
                                    user.Reputation = (Int32)ds.Tables[0].Rows[0]["Reputation"];
                                    user.RegTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["RegTime"]);
                                    if (ds.Tables[0].Rows[0]["DefDelivery"] == DBNull.Value)
                                    {
                                        user.DefaultDelivery = -1;
                                    }
                                    else
                                    {
                                        user.DefaultDelivery = (Int32)ds.Tables[0].Rows[0]["DefDelivery"];
                                    }
                                    Session.Add("User", user);
                                    Session.Timeout = 30;
                                    Response.Redirect("ActiveSuccess.aspx");
                                }
                                else
                                {
                                    //error_code=1
                                    //校验码错误，激活失败，请重新发送验证邮件！
                                    Response.Redirect("ActiveError.aspx?error=1");
                                }
                            }
                            else
                            {
                                //error_code=2
                                //校验码过期，请重新发送验证邮件！
                                Response.Redirect("ActiveError.aspx?error=2");
                            }
                        }
                        else
                        {
                            //error_code=3
                            //数据库无有效校验码，请重新发送验证邮件！
                            Response.Redirect("ActiveError.aspx?error=3");
                        }
                    }
                    else
                    {
                        if (Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]) == 1)
                        {
                            //error_code=4
                            //您的账号已经激活，请直接登录！
                            Response.Redirect("ActiveError.aspx?error=4");
                        }
                        else if (Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]) == 0)
                        {
                            //error_code=5
                            //您的账号已经被封！
                            Response.Redirect("ActiveError.aspx?error=5");
                        }
                        else
                        {
                            //error_code=6
                            //状态错误，请重新登录！ PS:不因该有这项
                            Response.Redirect("ActiveError.aspx?error=6");
                        }
                    }
                }
                else
                {
                    //error_code=7
                    //用户不存在！
                    Response.Redirect("ActiveError.aspx?error=7");
                }
            }
            else
            {
                //error_code=8
                //链接异常，请不要随便修改URL！
                Response.Redirect("ActiveError.aspx?error=8");
            }
        }
    }
}